// pages/carorder/carorder.js
var api = require('../../api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isticket:0,
    iscash:0,
    isorder:1,
    orderid:0,
    isnew:0,
    total:0,
    sf_total:0,
    chooseImg1: "../../images/car-right.png",
    chooseImg2: "../../images/car-right2.png",
    orderInfo: {}
  },

  nextpage: function(){
    var that=this;
    var pay_price=that.data.orderInfo.price;
    if (that.data.isticket>0)
    {
      pay_price = (that.data.orderInfo.price * 1.1).toFixed(2);
    }

    if (that.data.isnew==1)
    {
      if (pay_price-20>=0){
        pay_price = (pay_price-20).toFixed(2);
      }else{
        pay_price=0.01;
      }
    }
    if (that.data.iscash == 0){
        wx.request({
          url: api.ye_order_unifiedorder,
          method: 'POST',
          data: {
            departure: that.data.orderInfo.departure,
            destination: that.data.orderInfo.destination,
            usetime: that.data.orderInfo.usetime,
            openid: that.data.orderInfo.openid,
            cartype: that.data.orderInfo.cartype,
            price: pay_price,
            discount: that.data.orderInfo.discount,
            discount_notice: that.data.orderInfo.discount_notice,
            total: that.data.orderInfo.total,
            isticket: that.data.isticket,
            flight_id: that.data.orderInfo.flight_id,
            producttype: that.data.orderInfo.producttype,
            distance: that.data.orderInfo.distance,
            rider: that.data.orderInfo.rider,
          },
          success: function (res) {
            if (res.data.code == 0) {
              wx.redirectTo({
                url: '../carpaysuccess/carpaysuccess',
              })
            } else if (res.data.code == 2){
              wx.showToast({
                title: res.data.msg,
                icon: "none",
                duration: 2000
              })
              var bu_price=(res.data.bu_price).toFixed(2)
              that.wxpayorder(pay_price, 2, bu_price);
            }else {
              wx.showToast({
                title: res.data.msg,
                icon: "none",
                duration: 2000
              })
            }
          },
          fail: function (data) {
            console.log("ye pay  error");
          }
        })
    }else{
      console.log(that.data.orderInfo);
      that.wxpayorder(pay_price,1,0);
    }
    
  },
  wxpayorder: function (pay_price, pay_type, bu_price){
    var that=this;
    wx.request({
      url: api.order_unifiedorder,
      method: 'POST',
      data: {
        departure: that.data.orderInfo.departure,
        destination: that.data.orderInfo.destination,
        usetime: that.data.orderInfo.usetime,
        openid: that.data.orderInfo.openid,
        cartype: that.data.orderInfo.cartype,
        price: pay_price,
        pay_type: pay_type,
        discount: that.data.orderInfo.discount,
        discount_notice: that.data.orderInfo.discount_notice,
        total: that.data.orderInfo.total,
        isticket: that.data.isticket,
        flight_id: that.data.orderInfo.flight_id,
        producttype: that.data.orderInfo.producttype,
        distance: that.data.orderInfo.distance,
        rider: that.data.orderInfo.rider,
      },
      success: function (res) {
        wx.setStorage({
          key: 'orderInfo',
          data: {
            departure: that.data.orderInfo.departure,
            destination: that.data.orderInfo.destination,
            usetime: that.data.orderInfo.usetime,
            openid: that.data.orderInfo.openid,
            cartype: that.data.orderInfo.cartype,
            price: pay_price,
            pay_type: pay_type,
            bu_price: bu_price,
            flight_id: that.data.orderInfo.flight_id,
            isticket: that.data.isticket,
            producttype: that.data.orderInfo.producttype,
            distance: that.data.orderInfo.distance,
          },
        })
        if (res.data > 0) {
          wx.navigateTo({
            url: '../carpay/carpay?id=' + res.data,
          })
        } else if (res.data == 0) {
          wx.showToast({
            title: "手机号码填写有误,无法创建订单！",
            icon: "none",
            duration: 2000
          })
        }else{
          wx.showToast({
            title: "创建订单失败",
            icon: "none",
            duration: 2000
          })
        }
      },
      fail: function (data) {
        console.log("error");
      }
    })
  },
  chooseCar1: function () {
    this.data.iscash=1;
    console.log(this.data.iscash);
    this.setData({
      chooseImg1: "../../images/car-right2.png",
      chooseImg2: "../../images/car-right.png"
    })
  },
  chooseCar2: function () {
    this.data.iscash = 0;
    console.log(this.data.iscash);
    this.setData({
      chooseImg2: "../../images/car-right2.png",
      chooseImg1: "../../images/car-right.png"
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // wx.setStorage({
    //   key: 'orderInfo',
    //   data: {
    //     departure: "dfs",
    //     destination: "目的地123123",
    //     usetime: "2018-09-18 17:35:59",
    //     openid: "741852252cfhfg",
    //     cartype: "1",
    //     price: "88",
    //     flight_id:"2",
    //     producttype:"1"
    //     },
    //   })
    var orderInfo = wx.getStorageSync('orderInfo');
    console.log(orderInfo);
    this.data.orderInfo = orderInfo;
    this.setData({
      price:orderInfo.price,
      tax_price: (orderInfo.price * 1.1).toFixed(2)
    })

    var that = this;
    that.setData({
      total: that.data.orderInfo.price,
      sf_total: that.data.orderInfo.price,
    })
    /*
    wx.request({
      url: api.getuserinfo,
      method: 'POST',
      data: {
        openid: that.data.orderInfo.openid
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res.data);
        that.setData({
         isnew: res.data.isnew,
        })
        if (that.data.isnew == 1) {
          if (that.data.orderInfo.price - 20 >= 0) {
            that.setData({
              total: that.data.orderInfo.price,
              sf_total: (that.data.orderInfo.price - 20).toFixed(2),
            })
          } else {
            that.setData({
              total: that.data.orderInfo.price,
              sf_total: 0.01,
            })
          }
        }else{
          that.setData({
            total: that.data.orderInfo.price,
            sf_total: that.data.orderInfo.price,
          })
        }
      }
    });
   */
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }

})